auto();
var time = new Date().valueOf();
var 图集 = new Array()
图集[0] = images.read("/sdcard/脚本/图片/安装领取.jpg")
图集[1] = images.read("/sdcard/脚本/图片/点击领取.jpg")
图集[2] = images.read("/sdcard/脚本/图片/狠心放弃.jpg")
图集[3] = images.read("/sdcard/脚本/图片/直接领取.jpg")
图集[4] = images.read("/sdcard/脚本/图片/蓝水壶.jpg")
图集[5] = images.read("/sdcard/脚本/图片/关闭兑奖.jpg")
图集[6] = images.read("/sdcard/脚本/图片/继续种花.jpg")
图集[7] = images.read("/sdcard/脚本/图片/开心收下.jpg")
图集[8] = images.read("/sdcard/脚本/图片/去浇水.jpg")
图集[9] = images.read("/sdcard/脚本/图片/立即领取.jpg")
图集[10] = images.read("/sdcard/脚本/图片/金水壶.jpg")
图集[11] = images.read("/sdcard/脚本/图片/知道了.jpg")
图集[12] = images.read("/sdcard/脚本/图片/华农太忙.jpg")


var thread1 = threads.start(function () {

    while (true) {
        
        if (textContains("立即开始").findOne(100)) {
            click("立即开始")
        }


        if (textContains("今日内安装").findOne(100)) {
            log("不安装app")
            click(559, 338)
        }
        sleep(500)
    }
});
if (!requestScreenCapture()) {
    toast("请求截图失败");
    exit();
}
sleep(5000)
// thread1.interrupt()



var img = captureScreen();




app.startActivity({
    action: "android.intent.action.VIEW",
    data: 'hiapp://com.huawei.appmarket?activityName=activityUri|webview.activity&params={"params":[{"name":"url","type":"String","value":"' + encodeURIComponent("https://uowap.hicloud.com/appdl-uomp-cn/campaignpreview/af8e7e9a-c3be-4463-83e2-1f6d94f980d3/index.html?page=campaign&productId=&campaignId=46929&shareId=463783639952396032&shareTo=weixin&shareFrom=appmarket#/") + '"},{"name":"uri","type":"String","value":"external_webview"}]}&channelId=',
    packageName: "com.huawei.appmarket"
});

while (1) {
     if(time>3600000){
            threads.shutDownAll()
            }
    var img = captureScreen();

    //关闭签到框
    找色(img, "#fffffd", [[-17, 171, "#fb776a"], [-107, 209, "#f9f1e6"], [21, 214, "#f9f0e7"], [218, 1069, "#7feee5"], [307, 992, "#f42d26"]], "关闭签到框");
    click("邀请送花瓣");
    if (text("立即领取").findOne(10000)) {
        sleep(1000)
        click("立即领取");
    } else if (text("去邀请").findOne(2000)) {
        log("取消未邀请")
        click(544, 408);
        break;
    }

    var img = captureScreen();
    //关闭明日可领
    var p = images.findMultiColors(img, "#ffffff", [[-50, 175, "#783000"], [171, 192, "#773100"], [-178, 925, "#a2a2a2"], [-10, 917, "#a4a4a4"], [194, 929, "#a0a0a0"], [-56, 945, "#ffffff"], [-9, 949, "#ffffff"], [30, 949, "#ffffff"]]);
    if (p) {
        log("关闭明日可领");
        click(p.x, p.y)
        sleep(3000)
        break;
    } else {
        log("未发现明日可领")
    }
    //开心收下
    找色(img, "#fdb522", [[-2, -13, "#ffffff"], [0, 14, "#feebc9"], [-53, -1, "#ffffff"], [82, -3, "#ffffff"], [47, -6, "#fdba25"], [38, 7, "#fdb01e"], [1, -11, "#ffffff"]], "开心收下");
}
console.log("已完成邀请");

//点击采蜜
while (1) {
    if(time>3600000){
            threads.shutDownAll()
            }
    var img = captureScreen();

    //开心收下
    找色(img, "#fdb522", [[-2, -13, "#ffffff"], [0, 14, "#feebc9"], [-53, -1, "#ffffff"], [82, -3, "#ffffff"], [47, -6, "#fdba25"], [38, 7, "#fdb01e"], [1, -11, "#ffffff"]], "开心收下");
    //关闭签到框
    找色(img, "#fffffd", [[-17, 171, "#fb776a"], [-107, 209, "#f9f1e6"], [21, 214, "#f9f0e7"], [218, 1069, "#7feee5"], [307, 992, "#f42d26"]], "关闭签到框");



    //点击小蜜蜂
    var img = captureScreen();
    var p = images.findMultiColors(img, "#fac60d", [[49, -46, "#e2e9f4"], [5, -86, "#e2e9f4"], [-25, -7, "#fac60d"], [-50, 46, "#ecaf09"], [-24, 2, "#fac60d"]]);
    if (p) {
        console.log("点击采蜜");
        sleep(1000)
        click(p.x, p.y);
        click(p.x, p.y);
        break
    }
    sleep(500)
}
console.log("已点击去采蜜");
while (1) {
   if(time>3600000){
            threads.shutDownAll()
            }
    sleep(10);
    click("开始采蜜")
    click("拆水滴大礼包");
    click("继续免费挑战");
    //采蜜
    var img = captureScreen();
    var p = images.findMultiColors(img, "#ffd248", [[-40, 71, "#71960c"], [33, 59, "#688e09"], [103, 159, "#567c06"], [-104, 198, "#fec66d"]], {
        region: [452, 1100, 350, 230]
    });
    if (p) {

        click(871, 1631)
    }
    //杀虫
    p = images.findMultiColors(img, "#ffffff", [[40, -3, "#ffffff"], [28, 15, "#cd6c37"], [57, 15, "#ff9f49"], [162, 138, "#fec76f"], [-62, 152, "#fec66d"]], {
        region: [452, 1143, 350, 200]
    });
    if (p) {

        click(195, 1685)
    }
    if (text("明日再来").findOne(20)) {
        console.log("采蜜完成");
        back()
        break;
    }
}
console.log("已采蜜");

while (1) {
    if(time>3600000){
            threads.shutDownAll()
            }
    找图(图集, img)

    var 水量 = textContains("g").findOne(1000)
    if (textContains("g").findOne(2000)) {
        var 水量 = textContains("g").findOne(1000)
        log(水量)
        if ((水量.text().split("g")[0]) > 50) {
            click("g")
        } else {
            log("水量小于50，结束所有脚本")
            操作app("停止", "华为应用市场")
            操作app("停止", "华为教育中心")
            操作app("停止", "华为音乐")
            操作app("停止", "收音机")
            engines.stopAll()
        }
    }

    var 连续两天浇水 = textContains("连续两天浇水").findOne(1000)
    if (连续两天浇水) {
        //结束所有脚本
        log("结束所有脚本")
        操作app("停止", "华为应用市场")
        操作app("停止", "华为教育中心")
        操作app("停止", "华为音乐")
            操作app("停止", "收音机")
        engines.stopAll()

    }
    var 今日内安装 = textContains("今日内安装").findOne(1000)
    if (今日内安装) {
        //结束所有脚本
        log("不安装app")
        click(559, 338)
    }
    var 去领取 = textContains("去领取").findOne(100)
    if (去领取) {
        sleep(1000)
        click("去领取")
    }
    var 去消费 = textContains("去领取").findOne(100)
    if (去消费) {
        break;
    }
    click("立即领取")
    var img = captureScreen();
    
   

}
操作app("停止", "华为应用市场")
操作app("停止", "游戏中心")
操作app("停止", "华为音乐")
操作app("停止", "收音机")
操作app("停止", "华为收音机")
threads.shutDownAll()
卸载app()
function 找色(img, 颜色, 各坐标颜色, 日志) {
    //var img = captureScreen();
    var p = images.findMultiColors(img, 颜色, 各坐标颜色)
    if (p) {
        sleep(1000)
        console.log(日志);
        click(p.x, p.y);
        sleep(1000)
    }
}

function 区域找色(img, 颜色, 各坐标颜色, 区域坐标, 日志) {
    //var img = captureScreen();
    var p = images.findMultiColors(img, 颜色, 各坐标颜色, {
        region: 区域坐标
    })
    if (p) {
        sleep(1000)
        console.log(日志);
        click(p.x, p.y);
        sleep(1000)
    }
}

function 卸载app() {
    //sleep(30000);
    log("开始卸载");
    var pm = context.getPackageManager()
    var appList = pm.getInstalledApplications(0)

    for (let i = 0; i < appList.size(); i++) {
        var app_ = appList.get(i)
        var appInfo = {
            appName: app_.loadLabel(pm),
            packageName: app_.packageName,
            isSystemApp: app_.isSystemApp(),
            lastUpdateTime: pm.getPackageInfo(app_.packageName, 0).lastUpdateTime
        }
        if (appInfo.lastUpdateTime > time) {
            console.log(appInfo.appName + "卸载中");
            操作app("卸载",appInfo.appName);
            sleep(1000)
            while (!click("卸载"));
        }
    }



    sleep(15000);
    log("卸载完成");

}
function 操作app(操作, app名称) {
    var 包名 = getPackageName(app名称);
    log(包名)
    switch (操作) {
        case "启动":
            shell('am start ' + 包名, true);
            sleep(10000)
            break;
        case "清除":
            操作app("停止", "设置")
            log("停止设置完成")
            openAppSetting(包名);
            点击文字("存储");
            点击文字("删除数据");
            点击文字("确定");
            sleep(10000)
            break;
        case "卸载":
            shell('pm uninstall ' + 包名, true);
            sleep(10000)
            break;
        case "停止":
            shell('am force-stop ' + 包名, true);
            sleep(10000)
            break;
        default:
        log("默认")
    }
}


function 找图(图集, 截图) {
    for (var i = 0; i < 图集.length; i++) {

        var p = findImage(截图, 图集[i]);
        if (p) {
            log(p)
            var x = p.x * 1 + 图集[i].getWidth() / 2
            var y = p.y * 1 + 图集[i].getWidth() / 2
            log(图集[i].getWidth() / 2, 图集[i].getWidth() / 2)
            click(x, y)
        }

    }
    sleep(500)

}
